﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnitForVSTS.NUnitTest;
using System.Reflection;

// We are using multiple namespaces with muliple classes with the same name, different meaning
// Therefore some alliasses to improve clarity
using TestTarget = NUnitTest.UnitTest.Targets;
using Microsoft.VisualStudio.TestTools.Common;
using NUnitSide = NUnit.Core;
using System.IO;


namespace  NUnitForVSTS.NUnitTest.UT
{
    /// <summary>
    /// Summary description for ExecutionHandlerTest
    /// </summary>
    [TestClass]
    public class ExecutionHandlerTest
    {
        [TestMethod]
        public void CreateErrorResultTest()
        {
            Type testClassType = typeof(TestTarget.TestFixtureSetupCorrect);
            MethodInfo testMethod = testClassType.GetMethod("HasBeenSetup");

            DummyTest dummy = new DummyTest();
            dummy.TestName = new NUnitSide.TestName();

            dummy.TestName.FullName = testClassType.FullName + "." + testMethod.Name;
            dummy.TestName.Name = testMethod.Name;
            dummy.TestType = testClassType.FullName;
            Assembly assembly = testClassType.Assembly;

            UnitTest test = new UnitTest(dummy, assembly.Location);
            UnitTestResult result = ResultFactory.CreateErrorResult(test, "Break", Guid.Empty); ;
            Assert.AreEqual("Break", result.ErrorMessage);
        }

        [TestMethod]
        public void CreateErrorResultExceptionTest()
        {
            try
            {
                throw new Exception("Incompetent User Exception");
            }
            catch (Exception e)
            {
                Type testClassType = typeof(TestTarget.TestFixtureSetupCorrect);
                MethodInfo testMethod = testClassType.GetMethod("HasBeenSetup");

                DummyTest dummy = new DummyTest();
                dummy.TestName = new NUnitSide.TestName();

                dummy.TestName.FullName = testClassType.FullName + "." + testMethod.Name;
                dummy.TestName.Name = testMethod.Name;
                dummy.TestType = testClassType.FullName;
                Assembly assembly = testClassType.Assembly;

                UnitTest test = new UnitTest(dummy, assembly.Location);
                UnitTestResult result = ResultFactory.CreateErrorResult(test, e, Guid.Empty);
                Assert.AreEqual("Incompetent User Exception", result.ErrorMessage);
            }
        }
    }
}
